package com.lemo.shiro.realms;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.realm.AuthenticatingRealm;
import org.apache.shiro.util.ByteSource;

public class RealmSHA1SaltTwo extends AuthenticatingRealm {

	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken auToken) throws AuthenticationException {

		System.out.println("RealmSHA1SaltTwo");
		UsernamePasswordToken token = (UsernamePasswordToken) auToken;
		String username = token.getUsername();

		if ("lee".equals(username)) {
			System.out.println("用户不存在！");
			throw new UnknownAccountException("用户不存在");
		}
		if ("lemo".equals(username)) {
			throw new LockedAccountException("锁户");
		}
		Object principal = username;
		Object credentials = null;
		if ("admin".equals(username)) {
			credentials = "ce2f6417c7e1d32c1d81a797ee0b499f87c5de06";
		} else if ("user".equals(username)) {
			credentials = "073d4c3ae812935f23cb3f2a71943f49e082a718";
		}

		ByteSource credentialsSalt = ByteSource.Util.bytes(username);

		String realmName = getName();
		SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(principal, credentials, credentialsSalt,
				realmName);
		return info;
	}
	
	public static void main(String[] args) {
		int hashIterations = 1024;
		Object salt = ByteSource.Util.bytes("user");
		Object source = "123456";
		String algorithmName ="SHA1";
		SimpleHash simpleHash  =new SimpleHash(algorithmName, source, salt, hashIterations);
		System.out.println(simpleHash);
	}
}
